package org.wdcode.web.email;

import org.wdcode.web.params.WdWebParams;
import org.wdcode.common.constants.WdErrorMessage;
import org.wdcode.common.exception.CustomRuntimeException;
import org.wdcode.core.factory.BaseFactory;

/**
 * 获得Email接口实例的工厂类
 * @see org.wdcode.web.email.Email
 * @author WD
 * @since JDK6
 * @version 1.0 2009-07-17
 */
public final class EmailFactory extends BaseFactory<Email> {
	// 工厂
	private final static EmailFactory	FACTORY;

	/**
	 * 静态初始化
	 */
	static {
		FACTORY = new EmailFactory();
	}

	/**
	 * 返回工厂
	 * @return 工厂
	 */
	public final static EmailFactory getFactory() {
		return FACTORY;
	}

	/**
	 * 实例化一个新的EmailUtil实例 所有配置都用默认设置
	 * @return EmailUtil
	 */
	@Override
	public final Email newInstance() {
		return newInstance(WdWebParams.getEmailParse());
	}

	/**
	 * 实例化一个新的EmailUtil实例
	 * @param parse 使用哪个包发送Email 参照EmailParse
	 * @return mailUtil
	 */
	public final Email newInstance(EmailParse parse) {
		return newInstance(WdWebParams.getEmailHost(), WdWebParams.getEmailFrom(), WdWebParams.getEmailPassword(), parse);
	}

	/**
	 * 实例化一个新的EmailUtil实例
	 * @param host smtp服务器
	 * @param from 发送Email服务器地址
	 * @param password Email服务器密码
	 * @return EmailUtil
	 */
	public final Email newInstance(String host, String from, String password) {
		return newInstance(host, from, password, WdWebParams.getEmailParse());
	}

	/**
	 * 实例化一个新的EmailUtil实例
	 * @param host smtp服务器
	 * @param from 发送Email服务器地址
	 * @param password Email服务器密码
	 * @param parse parse 解析包
	 * @return EmailUtil
	 */
	public final Email newInstance(String host, String from, String password, EmailParse parse) {
		return newInstance(host, from, password, WdWebParams.getEmailAuth(), WdWebParams.getEmailEncoding(), parse);
	}

	/**
	 * 实例化一个新的EmailUtil实例
	 * @param host smtp服务器
	 * @param from 发送Email服务器地址
	 * @param password Email服务器密码
	 * @param auth 是否验证
	 * @param charset 编码格式
	 * @return EmailUtil
	 */
	public final Email newInstance(String host, String from, String password, boolean auth, String charset) {
		return newInstance(host, from, password, auth, charset, WdWebParams.getEmailParse());
	}

	/**
	 * 实例化一个新的EmailUtil实例
	 * @param host smtp服务器
	 * @param from 发送Email服务器地址
	 * @param password Email服务器密码
	 * @param auth 是否验证
	 * @param charset 编码格式
	 * @param parse 解析包
	 * @return EmailUtil
	 */
	public final Email newInstance(String host, String from, String password, boolean auth, String charset, EmailParse parse) {
		// 判断使用哪个包
		switch (parse) {
			case JavaMail:
				// Sun JavaMail
				return new EmailJava(host, from, password, auth, charset);
			case Apache:
				return new EmailApache(host, from, password, auth, charset);
			default:
				throw new CustomRuntimeException(WdErrorMessage.PARSE_NOT_RIGHT);
		}
	}

	/**
	 * 私有构造
	 */
	private EmailFactory() {
		super();
	}
}
